package com.lzx.tease.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.common.utils.R;
import com.lzx.tease.entity.Comments;
import com.lzx.tease.entity.vo.User;
import com.lzx.tease.feign.UserClient;
import com.lzx.tease.mapper.CommentsMapper;
import com.lzx.tease.service.CommentsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-12-20
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Resource
    private CommentsMapper commentsMapper;

    @Resource
    private UserClient userClient;

    //用户发表评论
    @Override
    public R saveComments(Comments comments) {
        comments.setCreateTime(new Date());
        commentsMapper.insert(comments);
        return R.ok().message("已发表评论");
    }

    //获取所有评论
    @Override
    public R getCommentsList() {
        List<Comments> list = commentsMapper.selectList(null).stream().peek((comments -> {
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(comments.getUserId()));
            comments.setUser(user);
        })).collect(Collectors.toList());
        return R.ok().data("list", list);
    }
}
